package com.patrol.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.patrol.auth.config.MyPasswordEncoder;
import com.patrol.auth.dao.AdminUserDao;
import com.patrol.auth.pojo.AdminUser;
import com.patrol.auth.service.AdminUserService;
import com.patrol.dao.RiskPointDao;
import com.patrol.pojo.RiskPoint;
import com.patrol.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 宋灿
 * @version 1.0
 * @description: TODO
 * @date 2021/8/12 16:43
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Resource
    private AdminUserDao adminUserDao;

    @Resource
    private MyPasswordEncoder myPasswordEncoder;

    @Override
    public boolean registerAdminUser(String username, String password) {
        AdminUser adminUser = new AdminUser();
        adminUser.setUsername(username);
        adminUser.setPassword(myPasswordEncoder.md5(password));
        adminUser.setAuthority(0);
        //使用findByUsername（）方法，判断userCheck是否为空（在数据库中是否存在）
        AdminUser userCheck = adminUserDao.findByUsername(adminUser.getUsername());
        if (userCheck == null) {
            adminUser.setAuthority(0);
            //不存在，执行插入操作，注册成功，返回首页面
            return 1 == adminUserDao.insert(adminUser);
        } else {
            return false;
        }
    }

    @Override
    public boolean loginAdminUser(String username, String password) {
        //把前端的username和password写入user对象，方便后续操作
        AdminUser user = new AdminUser();
        user.setUsername(username);
        user.setPassword(myPasswordEncoder.md5(password));
        //使用findByUsername_login（）方法，对数据库进行检查，判断账号密码是否正确
        AdminUser check_login = adminUserDao.findByUsername_login(username, myPasswordEncoder.md5(password));
        return check_login != null;
    }

    @Override
    public AdminUser selectUserByUserName(String loginUser) {
        QueryWrapper<AdminUser> qw = new QueryWrapper<>();
        qw.eq("username", loginUser);
        return adminUserDao.selectOne(qw);
    }

    @Override
    public boolean updatePwdByEmail(String email,String pwd) {
        return adminUserDao.updatePwd(myPasswordEncoder.md5(pwd), email);
    }

    @Override
    public boolean selectUsernameIsExist(String username) {
        QueryWrapper<AdminUser> qw = new QueryWrapper<>();
        qw.select("username", username);
        return null == adminUserDao.findByUsername(username);
    }
}
